package com.bocloud.cmp.dao.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.bocloud.cmp.dao.FlavorDao;
import com.bocloud.cmp.entity.resource.Flavor;
import com.bocloud.common.model.Param;
import com.bocloud.common.utils.Common;
import com.bocloud.common.utils.MapTools;
import com.bocloud.database.core.BasicDao;
import com.bocloud.database.utils.SQLHelper;

/**
 * 配置DAO接口实现
 * 
 * @author dongkai
 *
 */
@Repository("flavorDao")
public class FlavorDaoImpl extends BasicDao implements FlavorDao {

	@Override
	public List<Flavor> list(int page, int rows, List<Param> params, Map<String, String> sorter) throws Exception {
		String sql = "select * from flavor where 1=1";
		sql = SQLHelper.buildSql(sql, page, rows, params, sorter, "");
		Map<String, Object> paramMap = SQLHelper.getParam(params);
		List<Object> list = this.queryForList(sql, paramMap, Flavor.class);
		List<Flavor> flavors = new ArrayList<>();
		Flavor flavor = null;
		for (Object obj : list) {
			flavor = (Flavor) obj;
			flavors.add(flavor);
		}
		return flavors;
	}

	@Override
	public int count(List<Param> params) throws Exception {
		String sql = "select count(1) from flavor where 1=1";
		sql = SQLHelper.buildSql(sql, params, null, "");
		Map<String, Object> paramMap = SQLHelper.getParam(params);
		return this.countQuery(sql, paramMap).intValue();
	}

	@Override
	public Flavor getByFlavorId(String flavorId) throws Exception {
		String sql = "select * from flavor where flavor_id = :flavorId";
		Map<String, Object> params = MapTools.simpleMap("flavorId", flavorId);
		List<Object> list = this.queryForList(sql, params, Flavor.class);
		Flavor flavor = null;
		if (null != list && !list.isEmpty()) {
			flavor = (Flavor) list.get(0);
		}
		return flavor;
	}

	@Override
	public List<Flavor> listByVendorId(Long vendorId) throws Exception {
		String sql = "select * from flavor where vendor_id =:vendorId";
		Map<String, Object> params = MapTools.simpleMap("vendorId", vendorId);
		List<Object> list = this.queryForList(sql, params, Flavor.class);
		List<Flavor> flavors = new ArrayList<>();
		Flavor flavor = null;
		for (Object obj : list) {
			flavor = (Flavor) obj;
			flavors.add(flavor);
		}
		return flavors;
	}

	@Override
	public Flavor save(Flavor flavor) throws Exception {
		return (Flavor) this.saveEntity(flavor);
	}

	@Override
	public Flavor query(Long id) throws Exception {
		String sql = "select * from flavor where id = :id";
		Map<String, Object> params = MapTools.simpleMap(Common.ID, id);
		List<Object> list = this.queryForList(sql, params, Flavor.class);
		if (list.isEmpty()) {
			return null;
		}
		return (Flavor) list.get(0);
	}

	@Override
	public Boolean update(Flavor flavor) throws Exception {
		return this.updateEntity(flavor);
	}

	@Override
	public boolean removeByVid(Long vendorId) throws Exception {
		String sql = "delete from flavor where vendor_id = :vendorId";
		Map<String, Object> params = MapTools.simpleMap("vendorId", vendorId);
		return this.update(sql, params) > 0;
	}

	@Override
	public Boolean delete(Long id) throws Exception {
		String sql = "delete from flavor where id = :id";
		Map<String, Object> params = MapTools.simpleMap(Common.ID, id);
		return this.update(sql, params) > 0;
	}

	@Override
	public Flavor getByVid(Long vendorId, String name) throws Exception {
		String sql = "select * from flavor where vendor_id = :vendorId and name = :name";
		Map<String, Object> params = MapTools.simpleMap("vendorId", vendorId);
		params.put(Common.NAME, name);
		List<Object> list = this.queryForList(sql, params, Flavor.class);
		if (list.isEmpty()) {
			return null;
		}
		return (Flavor) list.get(0);
	}

	@Override
	public Flavor getByName(String name) throws Exception {
		String sql = "select * from flavor where name = :name";
		Map<String, Object> params = MapTools.simpleMap("name", name);
		List<Object> list = this.queryForList(sql, params, Flavor.class);
		Flavor flavor = null;
		if (null != list && !list.isEmpty()) {
			flavor = (Flavor) list.get(0);
		}
		return flavor;
	}

}
